package com.jzy.message.activities;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jzy.message.R;
import com.jzy.message.activities.adapter.ChengYuanAdapter;
import com.jzy.message.activities.base.AppConfig;
import com.jzy.message.activities.bean.ChengYuanBean;
import com.jzy.message.activities.bean.HuoQuBean;
import com.jzy.message.activities.bean.ReturnStatusResultEntity;

import java.util.ArrayList;
import java.util.List;

import static com.lzy.okhttputils.OkHttpUtils.getContext;

public class ChengyuanActivity extends AppCompatActivity {
    ChengYuanAdapter adapter;
    private ImageView iv_common_back;
    private TextView tv_common_title;
    private XRecyclerView list;
    private HuoQuBean bean;
    private ImageView img_addchengyuan;
    private int userid;
    private ChengYuanBean chengYuanBean;
    private List<ChengYuanBean.DataBean> beanList;
    private String room;
    private String username;
    private LinearLayoutManager linearLayoutManager;
    private LocalReceiver localReceiver;
    private IntentFilter intentFilter;
    private LocalBroadcastManager localBroadcastManager;
    private TextView tv_chengyuan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengyuan);
        initView();
        localBroadcastManager = LocalBroadcastManager.getInstance(ChengyuanActivity.this);
        intentFilter=new IntentFilter();
        intentFilter.addAction("shuaxinhaoyouliebiao");
        localReceiver=new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, intentFilter);


        userid=getIntent().getIntExtra("userid",0);
        room=getIntent().getStringExtra("room");
        username=getIntent().getStringExtra("username");
        getChengyuan();
        initLoad();
    }


    private void initView() {
        iv_common_back = (ImageView) findViewById(R.id.iv_common_back);
        tv_common_title = (TextView) findViewById(R.id.tv_common_title);
        tv_common_title.setText("群成员");
        iv_common_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img_addchengyuan = (ImageView) findViewById(R.id.img_addchengyuan);
        img_addchengyuan.setVisibility(View.VISIBLE);

        list = (XRecyclerView) findViewById(R.id.list);
        //addpople();

        linearLayoutManager=new LinearLayoutManager(getContext());
        list.setLayoutManager(linearLayoutManager);
        list.setLoadingMoreEnabled(false);
        tv_chengyuan = (TextView) findViewById(R.id.tv_chengyuan);
    }
    public void deletCheng(final int userid,String Roomname,final int position){
        String url= AppConfig.JASON_SERVICE_URL+"qlxroomuserrelation/deleteByUserID?userid="+
                userid+"&roomname="+Roomname;
        AppConfig.getInst().getHttpRequestJsonClass(url,"sss", ChengYuanBean.class, new AppConfig.Callback() {
            @Override
            public void onSuccess(Object o) {
                chengYuanBean=(ChengYuanBean) o;
                if (chengYuanBean.getStatus() == 1){
                    beanList.remove(position);
                    adapter.notifyDataSetChanged();
                    tv_chengyuan.setText("("+beanList.size()+")");
                }
            }
            @Override
            public void onFailure(ReturnStatusResultEntity error) {

            }
        });
    }
    public void getChengyuan(){
        String url= AppConfig.JASON_SERVICE_URL+"qlxroomuserrelation/querymemberByroom?roomName="+ room;
        AppConfig.getInst().getHttpRequestJsonClass(url,"sss", ChengYuanBean.class, new AppConfig.Callback() {
            @Override
            public void onSuccess(Object o) {
                chengYuanBean=(ChengYuanBean) o;
                if (chengYuanBean.getStatus() == 1){
                    beanList=chengYuanBean.getData();
                    tv_chengyuan.setText("("+beanList.size()+")");
                    adapter=new ChengYuanAdapter(beanList,ChengyuanActivity.this,userid);
                    list.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    img_addchengyuan.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ChengYuanBean serInfo = new ChengYuanBean(beanList);
                            Intent intent = new Intent();
                            Bundle bundle = new Bundle();
                            intent.putExtra("room",room);
                            intent.putExtra("username",username);
                            bundle.putSerializable("serinfo", serInfo);
                            intent.setClass(ChengyuanActivity.this, messegeQunLiaoActivity.class);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                } else
                {
                    beanList=new ArrayList<ChengYuanBean.DataBean>();
                    adapter = new ChengYuanAdapter(beanList, ChengyuanActivity.this, userid);
                    list.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }
                list.refreshComplete();
            }
            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                if(beanList!=null&&beanList.size()>0) {
                    beanList.clear();
                    adapter.notifyDataSetChanged();
                    list.refreshComplete();

                }
            }
        });
    }
    private void initLoad() {
        list.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url= AppConfig.JASON_SERVICE_URL+"qlxroomuserrelation/querymemberByroom?roomName="+ room;
                AppConfig.getInst().getHttpRequestJsonClass(url, "www", ChengYuanBean.class, new AppConfig.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        ChengYuanBean chengYuanBean=(ChengYuanBean) o;
                        if (beanList != null && beanList.size() > 0) {
                            beanList.clear();
                            beanList.addAll(chengYuanBean.getData());
                        }
                        tv_chengyuan.setText("("+chengYuanBean.getData().size()+")");
                        list.refreshComplete();
                    }
                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        list.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
            }
        });
    }
    //接受广播 回调mess页面
    class LocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.d("guanbao", "onReceive: ");
            getChengyuan();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);
    }
}
